package tool.soft

import akka.stream.Materializer
import dao._
import implicits.Implicits._
import models.Tables
import models.Tables._
import org.joda.time.DateTime
import play.api.data.FormBinding
import play.api.i18n.Messages
import play.api.libs.ws.WSClient
import play.api.mvc.{AnyContent, Request}
import play.twirl.api.Html
import shared.Pojo._
import shared.pojo.mission._
import tool.Pojo._
import tool._
import tool.soft.AllSoft.SoftBT
import upickle.default._
import utils.Utils

import java.io.File

/** Created by yz on 15/9/2020
  */
object HorizontalBoxplotSoft extends SoftBT with HorizontalBoxplotSoftT {

  override def getResultFileName: String = {
    "Diff_Metabolite.csv"
  }

  override def getFileInfos(workspaceDir: File, index: Int)(implicit
      matrixFileDataDao: MatrixFileDataDao,
      messages: Messages
  ): List[FileInfoData] = {
    List(
      FileInfoData("test_diff_filter_before.csv", "差异分析筛选前结果"),
      FileInfoData("test_diff.csv", "差异分析筛选后结果"),
      FileInfoData("abun.bar.csv", "abun bar数据文件"),
      FileInfoData("diff.mean.csv", "diff mean数据文件"),
      FileInfoData("Stamp.pdf", "结果图")
    )
  }

  override def getResultHtml(missionId: Int)(implicit request: Request[AnyContent], messages: Messages): Html = {
    views.html.user.mission.horizontalBoxplotResult(missionId)
  }

  override def produceConfigFile(workspaceDir: File, index: Int)(implicit
      request: Request[AnyContent],
      formBinding: FormBinding
  ) = {
    Utils.t2ConfigFileWithDefault(workspaceDir, data)
    plot.HorizontalBoxPlot.initConfigFile(workspaceDir, idStr = "")
  }

  def getArg(implicit request: Request[AnyContent], messages: Messages, formBinding: FormBinding): ujson.Value = {
    ujson.Obj()
  }

  override def getHtml(missionName: String, missionId: Option[Int])(implicit
      request: Request[AnyContent],
      messages: Messages
  ): Html = {
    views.html.user.soft.horizontalBoxplot(missionName, missionId)
  }

  override def getMission(implicit
      request: Request[AnyContent],
      matrixFileDataDao: MatrixFileDataDao,
      messages: Messages,
      formBinding: FormBinding
  ): Tables.MissionRow = {
    val input = List(data.file, data.groupFile)
    val argJson = getArg
    MissionRow(
      id = 0,
      missionName = s"${data.missionName}",
      kind = nameStr,
      userId = userId,
      startTime = new DateTime(),
      state = "preparing",
      arg = argJson,
      input = input
    )
  }

  override def produceInputFile(
      workspaceDir: File
  )(implicit request: Request[AnyContent], dao: MyDao, formBinding: FormBinding): Any = {
    val rawDataFile = new File(workspaceDir, "AllMet_Raw.txt")
    Tool.produceRawDataFile(userId, data.file, rawDataFile)
    val groupFile = Tool.getGroupFile(workspaceDir)
    Tool.produceRawGroupFile(userId, data.groupFile, rawDataFile, groupFile)
  }

  override def data(implicit request: Request[AnyContent], formBinding: FormBinding) = {
    Tool.transformRq.transform(reader[HorizontalBoxplotSoftData])
  }

  override def resultDeal(workspaceDir: File, resultDir: File)(implicit
      ws: WSClient,
      materializer: Materializer
  ): Unit = {
    super.resultDeal(workspaceDir, resultDir)
    workspaceDir.myListFiles.filter(_.isDirectory).foreach(_.dirCopyToDir(resultDir, Tool.fileNameFilter))
  }

  override def getCommandData(workspaceDir: File, resultDir: File, index: Int): List[CommandData] = {
    val solutionFile = new File(Tool.dataDir, "color_solution/color_solution_1.txt")
    val groupFile = new File(workspaceDir, "group.txt")
    val isMul = Tool.isMulGroup(workspaceDir)
    val testCommand = if (isMul) {
      s"""
         |${Tool.rScript} ${new File(Tool.rPath, "mul_group_p_value_calculate.R").unixPath}
         |""".stripMargin
    } else {
      s"""
         |${Tool.rScript} ${new File(Tool.rPath, "test_tb_calculate.R").unixPath}
         |""".stripMargin
    }
    val command =
      s"""
         |${Tool.rScript} ${Tool.rPath.unixPath}/produce_sample_color.R --g ${groupFile.unixPath}  --solution ${solutionFile.unixPath}
         |${Tool.rScript} ${Tool.rPath.unixPath}/fill_sample_color.R --g ${groupFile.unixPath}  --solution ${solutionFile.unixPath} --pc ${plot.HorizontalBoxPlot.plotO.configFileName}
         |$testCommand
           """.stripMargin
    val plotCommand = plot.HorizontalBoxPlot.getCommand(workspaceDir, "")
    List(CommandData(workspaceDir, List(command, plotCommand)))
  }

}
